package com.qitmiaojie.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qitmiaojie.pojo.Activity;
import com.qitmiaojie.pojo.dto.AdviceMsg;
import com.qitmiaojie.service.ActivityService;
import com.qitmiaojie.mapper.ActivityMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Slf4j
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>
    implements ActivityService{
    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public void insert(Activity activity) {
        activityMapper.insertActivity(activity);
    }

    @Override
    public IPage<Activity> selectByPage(Integer currentPage, Integer size, String name) {
        Page<Activity> page = new Page<>(currentPage,size);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }

        Page pages = baseMapper.selectPage(page, queryWrapper);
        return pages;
    }

    @Override
    public List<AdviceMsg> getMsg() {
        List<AdviceMsg> adviceMsgs = new ArrayList<>();
        List<Activity> activities = activityMapper.selectList();
        for (Activity activity : activities) {
            AdviceMsg adviceMsg = new AdviceMsg();
            adviceMsg.setMsg(activity.getName());
            adviceMsg.setDate(DateUtil.formatDate(activity.getEnddate()));
            adviceMsgs.add(adviceMsg);
        }
        return  adviceMsgs;
    }
}




